package com.c.ctgproject.ui.vehicleinspection

import android.annotation.SuppressLint
import android.app.ProgressDialog
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.entity.file.FirstExamineCarInfoEntity
import com.c.ctgproject.retrofit.Response
import com.c.ctgproject.ui.vehicleadmission.VehicleAdmissionActivity
import com.c.ctgproject.ui.vehicleadmission.VehicleAdmissionViewModel
import com.c.ctgproject.utils.*
import com.jaeger.library.StatusBarUtil
import kotlinx.android.synthetic.main.activity_vehicleinspection.*
import kotlinx.android.synthetic.main.title.*

/**
 * 车辆初检页面 入场前面
 */
class VehicleInspectionActivity : AppCompatActivity(), View.OnClickListener {
    private val mViewModel by lazy { ViewModelProvider(this)[VehicleInspectionViewModel::class.java] }
    private val statusColor: StatusColor = StatusColor()
    private var CarOwner: String? = null  //车主姓名
    private var OwnerPhone: String? = null //车主号码
    private var OwnerPlate: String? = null //车主号牌
    private var carId: String? = null //车主id
    private var salesman: String? = null //业务员
    private var progressDialog: ProgressDialog? = null
    private var vehicleAdmissionViewModel: VehicleAdmissionViewModel? = null
    private var dialogUtils: DialogUtils? = null
    private var token: String? = null
    private var dialog1: AlertDialog? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vehicleinspection)
        StatusBarUtil.setTranslucent(this)
        statusColor.setAndroidNativeLightStatusBar(this, false)
        toolbar_title.text = "车辆初检"
        setSupportActionBar(toolbar)
        supportActionBar?.setHomeButtonEnabled(true)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        event()

        vehicleAdmissionViewModel!!.getcarReturndata().observe(this, Observer { response: Response<*> ->
            if (response.status == 0) {
                progressDialog!!.dismiss()
                Toast.makeText(applicationContext, response.msg, Toast.LENGTH_SHORT).show()
                finish()
            } else {
                progressDialog!!.dismiss()
                dialogUtils!!.showTwo(this@VehicleInspectionActivity, "提示", response.data.handlerNull())
            }
        })
    }

    private fun event() {
        mViewModel.getInitialInspectionCar(intent.getStringExtra("ownerPlate").handlerNull())

        mViewModel.error.observe(this,  {
            MyLog.d("错误:$it")
            showToast(it)
        })

        mViewModel.result.observe(this,  {
            MyLog.d("初检详情:$it")
            handlerHttpResult(it){
                val data = initData(it)
                if (data.status == 0) {
                    //进入车辆初检
                    val entity = getGson().fromJson(it, FirstExamineCarInfoEntity::class.java).data
                    entity?.let {
//                        val intent = Intent(applicationContext, VehicleInspectionActivity::class.java)
//                        intent.putExtra("carOwner", entity.carOwner)
//                        intent.putExtra("ownerPhone", entity.ownerPhone)
//                        intent.putExtra("ownerPlate", entity.ownerPlate)
//                        intent.putExtra("carId", entity.carId)
//                        startActivity(intent)
                    }
                    if (entity == null) {
                        showToast("出现问题,请稍后再试")
                    }
                }
            }
        })


        progressDialog = ProgressDialog(
            this@VehicleInspectionActivity,
            R.style.AppTheme_Dark_Dialog
        )
        progressDialog!!.isIndeterminate = true
        progressDialog!!.setCanceledOnTouchOutside(false)
        progressDialog!!.setCancelable(true)
        dialogUtils = DialogUtils()
        vehicleAdmissionViewModel = ViewModelProvider(
            this, ViewModelProvider.AndroidViewModelFactory(application)
        ).get(VehicleAdmissionViewModel::class.java)

        token = Utils.getShared2(this, "token")
        CarOwner = intent.getStringExtra("carOwner")
        OwnerPhone = intent.getStringExtra("ownerPhone")
        OwnerPlate = intent.getStringExtra("ownerPlate")
        carId = intent.getStringExtra("carId")
        salesman = intent.getStringExtra("salesman")
        ownerPlate.text = OwnerPlate
        carOwner.text = CarOwner
        ownerPhone.text = OwnerPhone
        mSalesmanTV.text = salesman
        Backoff_button.setOnClickListener(this)
        confirm_button.setOnClickListener(this)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onClick(v: View?) {
        if (v == Backoff_button) {
            //退车
            showdialog()
        }
        if (v == confirm_button) {
            //入场事件
            val intent = Intent(applicationContext, VehicleAdmissionActivity::class.java)
            intent.putExtra("carId", carId)
            intent.putExtra("ownerPlate", OwnerPlate)
            startActivity(intent)
            finish()
        }
    }


    @SuppressLint("InflateParams")
    private fun showdialog() {
        val dialog = AlertDialog.Builder(this)
        val view = LayoutInflater.from(this.baseContext).inflate(R.layout.dialog_group, null, false)
        dialog.setView(view)
        dialog.setCancelable(false)
        val remark = view.findViewById<EditText>(R.id.remark)
        val confirm_bt = view.findViewById<Button>(R.id.confirm_bt)
        val cancel_bt = view.findViewById<Button>(R.id.cancel_bt)
        dialog1 = dialog.show()
        confirm_bt.setOnClickListener {
            if (TextUtils.isEmpty(remark.text.handlerNull())) {
                Toast.makeText(applicationContext, "备注不能为空！", Toast.LENGTH_SHORT).show()
            } else if (!NetworkUtils.isConnected(applicationContext)) {
                Toast.makeText(applicationContext, "请检查您的网络是否连接", Toast.LENGTH_SHORT).show()
            } else {
                progressDialog!!.setMessage("正在提交...")
                progressDialog!!.show()
                vehicleAdmissionViewModel!!.carReturn(
                    token!!,
                    carId!!,
                    remark.text.handlerNull()
                )

            }
        }
        cancel_bt.setOnClickListener { dialog1!!.dismiss() }
    }
}